home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ASTRONOM / H139.ZIP / UI101.ZIP / IO / UI / UI_TEST.C < prev    next >
C/C++ Source or Header  |  1991-11-04  |  12KB  |  544 lines

  1. /***************************************************************
  2.  
  3.     ui_test.c       Test GR and UI
  4.  
  5.             Copyright (c) 1991, Ted A. Campbell
  6.  
  7.             Bywater Software
  8.             P. O. Box 4023 
  9.             Duke Station 
  10.             Durham, NC  27706
  11.  
  12.             email: tcamp@hercules.acpub.duke.edu
  13.  
  14.     Copyright and Permissions Information:
  15.  
  16.     All U.S. and international copyrights are claimed by the
  17.     author. The author grants permission to use this code
  18.     and software based on it under the following conditions:
  19.     (a) in general, the code and software based upon it may be 
  20.     used by individuals and by non-profit organizations; (b) it
  21.     may also be utilized by governmental agencies in any country,
  22.     with the exception of military agencies; (c) the code and/or
  23.     software based upon it may not be sold for a profit without
  24.     an explicit and specific permission from the author, except
  25.     that a minimal fee may be charged for media on which it is
  26.     copied, and for copying and handling; (d) the code must be 
  27.     distributed in the form in which it has been released by the
  28.     author; and (e) the code and software based upon it may not 
  29.     be used for illegal activities. 
  30.  
  31. ***************************************************************/
  32.  
  33. /* #define  TEST_TW */
  34.  
  35. #include "stdio.h"
  36. #ifdef    __STDC__
  37. #include "malloc.h"
  38. #else
  39. extern char *malloc();
  40. #define size_t int
  41. #endif
  42. #include "bw.h"
  43. #include "gr.h"
  44. #include "kb.h"
  45. #include "dr.h"
  46. #ifdef TEST_TW
  47. #include "tw.h"
  48. #endif
  49. #include "ui.h"
  50.  
  51. #define TITLESIZE    64
  52. #define MAXENTRIES      64
  53.  
  54. #define MM_ITEMS        12              /* items in main menu */
  55.  
  56. char bw_ebuf[ BW_EBUFSIZE ];
  57.  
  58. struct uiwindow *big_box, *little_box, *d_box;
  59. struct menu_box m_box;
  60. struct dir_ent *d_entries[ MAXENTRIES ];
  61.  
  62. char *d_titles[ MAXENTRIES ];
  63.  
  64. char *t_titles[] = {
  65.    " Test Windows", 
  66.    " Test Colors",
  67.    " Test Styles", 
  68.    " Dialog Box", 
  69.    " Yes/No Box",
  70.    " Alarm Box ",
  71.    " Text File Selector",
  72.    " Icon File Selector ",
  73.    " Test Pixel Map ",
  74.    " Test Rubberbanding ",
  75. #ifdef TEST_TW
  76.    " Test GR Text Window ",
  77. #else
  78.    " [UNAVAILABLE] ",
  79. #endif
  80.    " Exit from Test" };
  81.  
  82. char tbuf[ 128 ];
  83. static char *buf;
  84.  
  85. main()
  86.    {
  87.    register int c, y;
  88.    int x1, y1, item;
  89.    
  90.    /* Initialize the screen and keyboard */
  91.  
  92.    /* Initialize the toolkit */
  93.  
  94.    ui_init();
  95.    if ( ui_grwind->initialized == FALSE )
  96.       {
  97.       printf( "The graphics system has not been initialized.\n" );
  98.       return -1;
  99.       }
  100.  
  101.    /* get memory for directory/menu arrays */
  102.  
  103.    for ( c = 0; c < MAXENTRIES; ++c )
  104.       {
  105.       if ( ( d_entries[ c ] =
  106.          (struct dir_ent *) malloc( sizeof( struct dir_ent ))) == NULL )
  107.          {
  108.          gr_deinit();
  109.          kb_deinit();
  110.          fprintf( stderr, "Cannot allocate memory for directory structure %d \n", c );
  111.          exit( 1 );
  112.          }
  113.       if ( ( d_titles[ c ] = malloc( TITLESIZE )) == NULL )
  114.          {
  115.          gr_deinit();
  116.          kb_deinit();
  117.          fprintf( stderr, "Cannot allocate memory for title array %d \n", c );
  118.          exit( 1 );
  119.          }
  120.       }         
  121.  
  122.    /* Clear the screen */
  123.  
  124.    ui_setscreen( GR_HIDDEN );
  125.    gr_cls( GR_HIDDEN );
  126.  
  127. #ifdef  OLD_DEBUG
  128.    fprintf( stderr, "Ready to draw main box\n" );
  129.    kb_rx();
  130. #endif
  131.  
  132.    big_box = ui_window( 0, 0, ui_grwind->xmax, ui_grwind->ymax,
  133.       TRUE, WHITE, BLACK,
  134.       "A Test of the Bywater Graphical User Interface", FALSE, BLACK,
  135.       FALSE, BLACK, WHITE, GRID, BUT_CLOSE | BUT_MOVE | BUT_RESIZE );
  136.  
  137. #ifdef  OLD_DEBUG
  138.       fprintf( stderr, "Drew main box\n" );
  139.       kb_rx();
  140. #endif
  141.  
  142. #ifdef OLD_DEBUG
  143.    sprintf( bw_ebuf, "imsize = %lx", gr_imsize( big_box->u_x1, big_box->u_y1,
  144.       big_box->u_x2, big_box->u_y2 ) );
  145.    bw_debug( bw_ebuf );
  146. #endif
  147.  
  148.    /* initialize the clock */
  149.  
  150. /*        ui_clinit( big_box->u_x1, big_box->u_y1, BLACK, WHITE ); */
  151.  
  152.       /* blit to GR_PRIMARY and switch to primary screen */
  153.  
  154.       gr_blit( GR_HIDDEN, GR_PRIMARY, 0, 0, ui_grwind->xmax,
  155.      ui_grwind->ymax );
  156.       ui_setscreen( GR_PRIMARY );
  157. #ifdef  OLD_DEBUG
  158.       kb_rx();
  159. #endif
  160.  
  161.       x1 = ui_grwind->xmax / 20;
  162.       y1 = ui_grwind->ymax / 10;
  163.  
  164.    /* main program loop, y == TRUE */
  165.  
  166.    y = TRUE;
  167.    m_box.is_drawn = FALSE;
  168.    while( y == TRUE )
  169.       {
  170.  
  171. #ifdef  OLD_DEBUG
  172.       bw_message( "Ready to activate list box" );
  173.       kb_rx();
  174. #endif
  175.  
  176.       /* clear the user area */
  177.  
  178.       ui_fbox( big_box->u_x1, big_box->u_y1, big_box->u_x2, big_box->u_y2,
  179.      BLACK, SOLID );
  180.       ui_fbox( big_box->u_x1, big_box->u_y1, big_box->u_x2, big_box->u_y2,
  181.      WHITE, GRID );
  182.  
  183.  
  184.       /* call the menu */
  185.  
  186.       m_box.is_drawn = FALSE;
  187.       item = ui_list( MENU_SLIDERS,
  188.      x1 * 2, y1 * 4, x1 * 10, big_box->u_y2 - y1,
  189.      "Test Options", MM_ITEMS, t_titles, BLACK, WHITE, WHITE,
  190.      &m_box );
  191.  
  192.       /* save the screen before executing */
  193.  
  194. #ifdef OLD_DEBUG
  195.       sprintf( bw_ebuf, "big_box = %08lx, x1 = %d", (long) big_box,
  196.      big_box->u_x1 );
  197.       bw_debug( bw_ebuf );
  198.       bw_message( " " );
  199. #endif
  200.  
  201.       /* gr_save( GR_PRIMARY, TRUE, big_box->u_x1, big_box->u_y1,
  202.      big_box->u_x2, big_box->u_y2, buf ); */
  203.  
  204.       /* evaluate the item chosen */
  205.  
  206.       switch( item )
  207.          {
  208.          case 0:
  209.             windows();
  210.             break;
  211.          case 1:
  212.             colors();
  213.             break;
  214.          case 2:
  215.             styles();
  216.             break;
  217.          case 3:
  218.             dialog();
  219.             break;
  220.          case 4:
  221.             yes_no();
  222.             break;
  223.          case 5:
  224.             test_alarm();
  225.             break;
  226.          case 6:
  227.             text_file();
  228.             break;
  229.          case 7:
  230.             icon_file();
  231.             break;
  232.          case 8:
  233.         pixmap();
  234.         ui_wait();
  235.             break;
  236.          case 9:
  237.             test_rband();
  238.         break;
  239.      case 10:
  240. #ifdef TEST_TW
  241.         test_tw();
  242. #endif
  243.         break;
  244.          case TK_EXIT:
  245.      case 11:
  246.             y = FALSE;
  247.             break;
  248.          }
  249.  
  250.       /* restore screen */
  251.  
  252. #ifdef OLD_DEBUG
  253.       sprintf( bw_ebuf, "big_box = %08lx, x1 = %d", (long) big_box,
  254.      big_box->u_x1 );
  255.       bw_debug( bw_ebuf );
  256.       bw_message( " " );
  257. #endif
  258.  
  259.       /* gr_save( GR_PRIMARY, FALSE, big_box->u_x1, big_box->u_y1,
  260.      big_box->u_x2, big_box->u_y2, buf ); */
  261.  
  262.       }
  263.  
  264.    /* Clear screen and restore prior state of computer */
  265.  
  266.    gr_cls( GR_PRIMARY );
  267.    kb_deinit();
  268.    gr_deinit();
  269.  
  270.    }
  271.  
  272. windows()
  273.    {
  274.    int x, y;
  275.  
  276.    x = ui_grwind->xmax / 20;
  277.    y = ui_grwind->ymax / 10;
  278.    little_box = ui_window( x * 2, y, x * 14, y * 7, 1, 1, 0,
  279.       "A Full-Featured Window",
  280.       1, 0, 1, 0, 1, 1, BUT_CLOSE | BUT_MOVE | BUT_RESIZE );
  281.  
  282.    ui_wait();
  283.  
  284.    /* Experiment:  title too long */
  285.  
  286.    little_box = ui_window( x * 2, y, x * 8, y * 5, TRUE, WHITE, BLACK,
  287.       "A Full-Featured Window with a Rather Extremely Long Title",
  288.       TRUE, BLACK, TRUE, BLACK, WHITE, SOLID, 0 );
  289.  
  290.    ui_wait();
  291.  
  292.    /* Write text to this window */
  293.  
  294.    ui_text( little_box->u_x1, little_box->u_y1,
  295.       little_box->u_x2, little_box->u_y2, 
  296.       29, 1, 0, 
  297.       "This line of text has been written into this box utilizing the ui_text() function. We need to write a really long string to it\n" );
  298.  
  299.    ui_wait();
  300.  
  301.    }
  302.  
  303. colors()
  304.    {
  305.    register int c;
  306.    int x, y;
  307.  
  308.    x = ui_grwind->xmax / 20;
  309.    y = ui_grwind->ymax / 10;
  310.  
  311.    for ( c = 0; c < gr_colors; ++c )
  312.       {
  313.       sprintf( ui_tbuf, "This is color %d", c );
  314.       little_box = ui_window( x * 2, y, x * 14, y * 6, TRUE, WHITE,
  315.      BLACK, ui_tbuf,
  316.      TRUE, BLACK, TRUE, BLACK, c, SOLID, 0 );
  317.       ui_wait();
  318.       }
  319.  
  320.  
  321.    }
  322.  
  323. styles()
  324.    {
  325.    register int c;
  326.    int x, y;
  327.  
  328.    x = ui_grwind->xmax / 20;
  329.    y = ui_grwind->ymax / 10;
  330.  
  331.    for ( c = 0; c < 4; ++c )
  332.       {
  333.       sprintf( ui_tbuf, "This is style %d", c );
  334.       little_box = ui_window( x * 2, y, x * 14, y * 6, TRUE, WHITE,
  335.      BLACK, ui_tbuf, TRUE, BLACK, TRUE, BLACK, WHITE, c, 0 );
  336.       ui_wait();
  337.       }
  338.  
  339.    }
  340.  
  341. dialog()
  342.    {
  343.    char d_buf[ 64 ];
  344.    int x, y;
  345.  
  346.    x = ui_grwind->xmax / 20;
  347.    y = ui_grwind->ymax / 10;
  348.  
  349.    ui_dial( x * 2, y, x * 14, y * 6, WHITE, BLACK, BLACK,
  350.       TRUE, "A Test Dialog Box",
  351.       "Please enter the name of your favorite saint \n(may be Orthodox or Catholic). No Anglicans accepted, or even Protestant or Evangelical Saints.  Thanks very much for your help.", 
  352.       "Your saint:  ", d_buf, &d_box );
  353.    sprintf( ui_tbuf, "Saint selected: %s ", d_buf );
  354.    ui_wtitle( d_box, ui_tbuf );
  355.    ui_wait();
  356.    }
  357.  
  358. yes_no()
  359.    {
  360.    int x, y, r;
  361.  
  362.    x = ui_grwind->xmax / 20;
  363.    y = ui_grwind->ymax / 10;
  364.  
  365.    r = ui_yn( x * 2, y, x * 14, y * 6, WHITE, BLACK, BLACK, TRUE,
  366.       "Do you want $ 1,000,000.00 in cash?", "  NO  ", "  YES  ",
  367.       &d_box );
  368.    }
  369.  
  370. test_alarm()
  371.    {
  372.    int x, y, r;
  373.  
  374.    x = ui_grwind->xmax / 20;
  375.    y = ui_grwind->ymax / 10;
  376.  
  377.    r = ui_alarm( x * 2, y, x * 14, y * 6, WHITE, BLACK, BLACK, TRUE,
  378.       "An alarm box like this would be used in case of a serious program or system problem.",
  379.       "  OK  ",
  380.       &d_box );
  381.    }
  382.  
  383. ui_poll()
  384.    {
  385. /*        ui_clock(); */
  386.    }
  387.  
  388. bw_error( s )
  389.    char *s;
  390.    {
  391.    char buf[ BW_EBUFSIZE ];
  392.  
  393.    sprintf( buf, "ERROR: %s   ", s );
  394.    gr_text( GR_PRIMARY, 5, 5, buf, BLACK, WHITE );
  395.    ui_wait();
  396.    }
  397.  
  398. #ifdef  DEBUG
  399. bw_debug( s )
  400.    char *s;
  401.    {
  402.    char buf[ BW_EBUFSIZE ];
  403.  
  404.    sprintf( buf, "DEBUG: %s     ", s );
  405.    gr_text( GR_PRIMARY, 5, 5, buf, BLACK, WHITE );
  406.    ui_wait();
  407.    }
  408.  
  409. #endif
  410.  
  411. bw_message( s )
  412.    char *s;
  413.    {
  414.    char buf[ BW_EBUFSIZE ];
  415.  
  416.    sprintf( buf, "MESSAGE: %s   ", s );
  417.    gr_text( GR_PRIMARY, 5, 5, buf, BLACK, WHITE );
  418.    }
  419.  
  420. text_file()
  421.    {
  422.    static struct menu_box m_box;
  423.    int x, y, item;
  424.    register int c, test;
  425.  
  426.    x = ui_grwind->xmax / 20;
  427.    y = ui_grwind->ymax / 10;
  428.  
  429.    m_box.is_drawn = FALSE;
  430.    item = ui_ftext( x * 2, y * 2, x * 14, y * 7, dr_all, "File Selector Test",
  431.       &m_box, d_titles, d_entries, MAXENTRIES, WHITE, BLACK, WHITE );
  432.  
  433.    if ( ( c != TK_ERROR ) && ( c != TK_EXIT ))
  434.       {
  435.       sprintf( ui_tbuf, "File selected: %s ",
  436.          d_entries[ item ]->filename );
  437.       ui_wtitle( m_box.window, ui_tbuf );
  438.       ui_wait();
  439.       }
  440.  
  441.    }
  442.  
  443. icon_file()
  444.    {
  445.    static struct menu_box m_box;
  446.    int x, y, item;
  447.  
  448.    x = ui_grwind->xmax / 20;
  449.    y = ui_grwind->ymax / 10;
  450.  
  451.    m_box.is_drawn = FALSE;
  452.    item = ui_ficon( x * 3, y * 2, x * 13, y * 9, dr_all, "TEST: ui_ficon() ",
  453.       &m_box, d_entries, MAXENTRIES, WHITE, BLACK, WHITE );
  454.  
  455.    if ( ( item != TK_ERROR ) && ( item != TK_EXIT ))
  456.       {
  457.       sprintf( ui_tbuf, "File selected: %s ",
  458.      d_entries[ item ]->filename );
  459.       ui_wtitle( m_box.window, ui_tbuf );
  460.       ui_wait();
  461.       }
  462.  
  463.    }
  464.  
  465. pixmap()
  466.    {
  467.    static struct pbm_struct pmstruct;
  468.    int x, y;
  469.  
  470.    x = ui_grwind->xmax / 20;
  471.    y = ui_grwind->ymax / 10;
  472.  
  473.    ui_pbmread( "test.pbm", &pmstruct, WHITE, BLACK );
  474.  
  475.    ui_pbmshow( GR_PRIMARY, x * 2, y * 2, &pmstruct );
  476.  
  477.    }
  478.  
  479. test_rband()
  480.    {
  481.    static int x1, y1, x2, y2;
  482.    
  483.    /* fill the large area with grid */
  484.  
  485.    ui_fbox( big_box->u_x1, big_box->u_y1, big_box->u_x2, big_box->u_y2, BLACK, SOLID );
  486.    ui_fbox( big_box->u_x1, big_box->u_y1, big_box->u_x2, big_box->u_y2, WHITE, GRID );
  487.  
  488.    /* get a rubber band rectangle */
  489.  
  490.    x1 = 20;
  491.    x2 = 200;
  492.    y1 = 20;
  493.    y2 = 200;
  494.    ui_rband( TRUE, &x1, &y1, &x2, &y2 );
  495.  
  496.    ui_fbox( x1, y1, x2, y2, BLACK, SOLID );
  497.    ui_fbox( x1 + 1, y1 + 1, x2 - 1, y2 - 1, WHITE, SOLID );
  498.  
  499.    bw_message( "Box now received: press a key or click to move it: " );
  500.    ui_wait();
  501.  
  502.    /* move it */
  503.  
  504.    ui_rband( FALSE, &x1, &y1, &x2, &y2 );
  505.  
  506.    ui_fbox( x1, y1, x2, y2, BLACK, SOLID );
  507.    ui_fbox( x1 + 1, y1 + 1, x2 - 1, y2 - 1, WHITE, SOLID );
  508.  
  509.    /* wait */
  510.  
  511.    bw_message( " Box now moved; press a key or click for main menu" );
  512.    ui_wait();
  513.    
  514.    }
  515.  
  516. #ifdef TEST_TW
  517. test_tw()
  518.    {
  519.    int c;
  520.    int x, y;
  521.    struct ui_twstruct *uibw;
  522.  
  523.    x = ui_grwind->xmax / 20;
  524.    y = ui_grwind->ymax / 10;
  525.  
  526.    uibw = ui_twinit( "Test Text Window",
  527.         x * 4, y * 6, 12, 52, 0, 0,
  528.         ui_grwind->xmax, ui_grwind->ymax );
  529.  
  530.    tw_cursor( TRUE );
  531.  
  532.    c = 0;
  533.    while ( c != 0x1b )
  534.       {
  535.       c = kb_rx();
  536.       tw_outc( c );
  537.       }
  538.  
  539.    tw_cursor( FALSE );
  540.    }
  541.  
  542. #endif
  543.  
  544.